<template>
  <Container class="manage-list role-list">
    <div class="container-header group" slot="header">
      <Input
        class="start"
        v-model.trim="keyword"
        placeholder="类名"
        @on-enter="onSearchBtnClick">
      </Input>
      <Button type="primary" icon="ios-search" class="btn-search" @click="onSearchBtnClick"></Button>
    </div>
    <Tabs v-model="tab">
      <Button
        size="large"
        class="item"
        type="primary"
        slot="extra"
        icon="md-add"
        @click="onAddRole">新建基础属性</Button>
      <TabPane label="基础属性列表" name="tab1">
        <Table
          :height="options.tableHeight"
          stripe
          :columns="list.columns"
          :data="list.data"
          @on-selection-change="onSelectChange"
          no-data-text="亲，没有找到相关记录哦！~"></Table>
        <div class="pager">
          <Page
          :total="list.totalItem"
          :current="list.currentPage"
          :page-size="list.pageSize"
          @on-change="onPageChange"
          @on-page-size-change="onPageSizeChange"
          show-sizer
          placement="top"></Page>
        </div>
        <Loader :status="list.status" @refresh="getData"></Loader>
      </TabPane>
    </Tabs>
  </Container>
</template>
<script>
import table from '@/table'
import AdminListData from './list/BaseListData'
import { clearEmpty } from '@/util'
export default {
  name: 'setting/role/list',
  data() {
    return {
      ids: [],
      list: AdminListData,
      tab: 'tab1',
      keyword: '',
      options: {
        tableHeight: 300
      }
    }
  },
  mounted() {
		this.list.currentPage = ~~this.$route.query.currentPage || 1
    this.list.pageSize = ~~this.$route.query.pageSize || this.list.pageSize
    this.initTable()
    this.$bus.$on('onEdit', this.onEdit)
    this.$bus.$on('onDelete', this.onDelete)
    this.getData()
  },
  destroyed() {
    this.$bus.$off()
  },
  methods: {
    ...table,
    getData() {
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        type_name: this.keyword
      }
      param = clearEmpty(param)
      this.list.status = 'loading'
      this.$ajaxGet('/system/list', param).then(data => {
        this.list.data = (data.data || []).map(item => {
          let target = {
            type: item.type,
            type_name: item.type_name,
            name: item.name,
            is_te: item.is_te,
            id: item.id,
            add_time: item.add_time
          }
          return target
        })
        this.list.totalItem = Number(data.total)
        this.list.status = 'pass'
        this.backTableTop()
      }).catch(() => {
        this.list.status = 'error'
      })
    },
    onSearchBtnClick() {
      this.list.currentPage = 1
      this.getData()
    },
    onAddRole() {
      this.$router.push('/setting/base/edit')
    },
    onDelete(id, isSystem, name) {
      this.$Modal.confirm({
        title: '删除',
        content: '确定删除当前基础属性？',
        onOk: () => {
          let data = {
            type_id: id
          }
          this.$jsonPost('/system/delType', data).then(() => {
            this.$Notice.success({
              title: '删除成功',
              desc: `<span class='font-red'>${name}</span> 已被删除。`
            })
            this.getData()
          })
        }
      })
    },
    onSelectChange(selection) {
      this.ids = selection.map(item => item.id)
    },
    onPageChange(number) {
			this.list.currentPage = number
			this.$router.push({
				name: 'setting/base/list',
				query: {
					currentPage: this.list.currentPage,
					pageSize: this.list.pageSize
				}
			})
      this.getData()
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1
			this.list.pageSize = pageSize
			this.$router.push({
				name: 'setting/base/list',
				query: {
					currentPage: this.list.currentPage,
					pageSize: this.list.pageSize
				}
			})
			this.getData()
    },
    onEdit(id) {
      this.$router.push({
        path: '/setting/base/edit',
        query: { id: id }
      })
    }
	},
	watch: {
		$route() {
			let page = ~~this.$route.query.currentPage
			let table = this.list
			if (table.currentPage !== page) {
				table.currentPage = ~~this.$route.query.currentPage === 0 ? 1 : ~~this.$route.query.currentPage
				this.getData()
			}
		}
	}
}
</script>